package com.zhj.leetcode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 力扣 933 变化：利用队列计时过期删除
 * @author zhj
 */
public class Test933Plus {
    public static void main(String[] args) throws Exception {
        Counter c = new Counter();
        for (int i = 0; i < 30; i++) {
            Thread.sleep(2000);
            int count = c.ping(System.currentTimeMillis());
            if (count > 3) {
                System.out.println("十秒内只允许访问3次");
                Thread.sleep(3000);
                continue;
            }
            System.out.println("成功访问！！！");
        }


    }
}
class Counter {
    private Queue<Long> queue;

    public Counter() {
        queue = new LinkedList<>();
    }

    int ping(long t) {
        queue.add(t);
        // 计算符合3000毫秒以上出队列
        while (!queue.isEmpty() && t - queue.peek() > 10000) {
            queue.poll();
        }
        return queue.size();
    }
}